package com.wingy.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EditActivity extends AppCompatActivity {

    private static final String TAG = "Diary";
    public static final String EDIT_DIARY_ACTION = "DiaryEditor.EDIT_DIARY";
    public static final String INSERT_DIARY_ACTION = "DiaryEditor.action.INSERT_DIARY";
    private static final int STATE_INSERT = 1;
    private int mState;
    private EditText mTitleText;
    private EditText mBodyText;
    private long mid;
    private Button confirm;
    private LifeDiary lifeDiary = new LifeDiary(null, null);
    private List<LifeDiary> lifeDiaryList = new ArrayList<>();
    private static ConfigFlie configFlie = new ConfigFlie(true, "");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        final Intent intent = getIntent();
        final String action = intent.getAction();

        ConfigFlie temp = new ConfigFlie(true, "");
        //configFlie.Saveconfig(getFilesDir().getAbsolutePath(), configFlie);
        if(temp.Readconfig(getFilesDir().getAbsolutePath()) != null) {
            configFlie = temp.Readconfig(getFilesDir().getAbsolutePath());
        }

        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        confirm = (Button) findViewById(R.id.confirm);

        if (EDIT_DIARY_ACTION.equals(action)) {// 编辑日记
            Bundle bundle = new Bundle();
            bundle = this.getIntent().getExtras();
            mid = bundle.getLong("id");

            lifeDiaryList = lifeDiary.readLifediary(getFilesDir().getAbsolutePath(), configFlie.getPWD());

            setTitle("编辑日记");
            mTitleText.setTextKeepState(lifeDiaryList.get((int) mid).getTitle());
            mBodyText.setTextKeepState(lifeDiaryList.get((int) mid).getBody());
            confirm.setText("改好了");

        } else if (INSERT_DIARY_ACTION.equals(action)) {// 新建日记
            mState = STATE_INSERT;
            setTitle("新建日记");
        } else {
            Log.e(TAG, "no such action error");
            finish();
            return;
        }

        confirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mState == STATE_INSERT) {
                    insertDiary();
                } else {
                    updateDiary();
                }
                Intent intent = new Intent(EditActivity.this, MainActivity.class);
                startActivity(intent);
                EditActivity.this.finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(EditActivity.this, MainActivity.class);
        startActivity(intent);
        super.onBackPressed();
    }

    private void insertDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());

        lifeDiary.newLifeDairy(getFilesDir().getAbsolutePath(), title, body, date, configFlie.getPWD());

    }

    private void updateDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());

        lifeDiary.updateLifediary(getFilesDir().getAbsolutePath(), (int)mid, title, body, date, configFlie.getPWD());
    }

}
